package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.CodeUpdateDto;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.vo.CodeTypeResult;
import com.tcm.entity.resp.R;
import com.tcm.exception.DatabaseOperationException;
import com.tcm.exception.EntityAlreadyExistException;
import com.tcm.service.CodeService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 码表管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/3 16:01
 */
@Slf4j
@RestController
@RequestMapping("/pc/code")
public class CodeController {

    @Autowired
    private CodeService codeService;

    /**
     * 添加码表
     * @param request http请求对象
     * @param code 码表更新Dto
     * @return 响应添加结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addCode(HttpServletRequest request, @RequestBody @Valid CodeUpdateDto code){
        System.out.println(code);
        Long userId = AccessTokenUtil.getUserId(request);
        boolean res = false;
        try {
            res = codeService.addCode(code, userId);
        } catch (EntityAlreadyExistException e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 获取码表类型列表
     * @return 码表类型列表
     */
    @RequestMapping(value = "/type", method = RequestMethod.GET)
    public R getCodeTypeList(){
        List<CodeTypeResult> codeType = codeService.getCodeType();
        return R.success(codeType);
    }

    /**
     * 分页查询码表
     * @param type 码表类型
     * @param page 当前页号
     * @param pageSize 页面大小
     * @return 分页查询结果
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getCodeListByPage(String type, Integer page, Integer pageSize){
        PageR<CodeUpdateDto> codeListByPage = codeService.getCodeListByPage(page, pageSize, type);
        return R.success(codeListByPage);
    }

    /**
     * 删除码表接口
     * @param ids id集合
     * @param request http请求对象
     * @return 响应删除
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeCodes(@RequestParam("ids") List<Long> ids, HttpServletRequest request){
        // 判断参数是否为空
        if (ids.isEmpty()) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }

        boolean res = false;
        try {
            res = codeService.dropCodeByIds(ids, AccessTokenUtil.getUserId(request));
        } catch (DatabaseOperationException e) {
            return R.fail(ResponseEnum.PARAM_IS_INVAlID);
        }
        return res ? R.success() : R.fail(ResponseEnum.HANDLE_FAIL);
    }

    /**
     * 修改码表信息接口
     * @param codeUpdateDto 码表信息封装对象
     * @param request http请求对象
     * @return 响应对象
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyCode(@RequestBody @Valid CodeUpdateDto codeUpdateDto, HttpServletRequest request){
        if (codeUpdateDto.getId() == null){
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        Long userId = AccessTokenUtil.getUserId(request);
        boolean res = false;
        try {
            res = codeService.modifyCode(codeUpdateDto, userId);
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

}
